<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Admin\Controller;
use Think\Controller;
use Org\Util\Rbac;

/**
 * 后台 父级控制器
 * Class AdminController
 * @package Admin\Controller
 */
class AdminController extends Controller{

    /**
     * 后台控制器初始化操作
     * @return bool
     */
    public function _initialize(){
        // 获取当前用户ID
        if(defined('UID')) return ;
        define('UID',is_login());
        if( !UID ){// 还没登录 跳转到登录页面
            $this->redirect('Public/login');
        }
        trace(session('user_auth'),'user_auth');
        //读取数据库中的配置
        $config     =   S("DB_CONFIG");
        if(!$config){
            $Config     =   new \Common\Tool\Config();
            $config =   $Config::lists();//解析的配置数据
            trace($config,'config');
            S('DB_CONFIG',$config);
        }
        C($config);

    }



    /**
     * 修改登录名与密码
     */
    public function update(){
        if(IS_POST){
            $type = I("post.type");
            $Admin = D("Admin");
            if('username' == $type){
                //修改名称
                $data['username'] = I("post.username");
                $data['uid'] = session("user_auth.uid");
                trace($data,'data');
                if($Admin->updateUsername($data)){
                    $this->success("修改成功");
                }else{
                    $this->error("修改失败 ".$Admin->getError());
                }
            }elseif('password'== $type){
                //修改密码
                $data['uid'] = session("user_auth.uid");
                $data['password'] = I("post.password");
                $data['password2'] = I("post.password2");
                if($Admin->updatePassword($data)){
                    $this->success("修改成功");
                }else{
                    $this->error("修改失败 ".$Admin->getError());
                }
            }
        }else{
            $username = session("user_auth.username");
            $this->assign('username', $username);
            $this->meta_title = '修改登录名与密码';
            $this->display('update');
        }
    }

    /**
     * 修改昵称提交
     * @author huajie <banhuajie@163.com>
     */
    public function submitNickname(){
        //获取参数
        $nickname = I('post.nickname');
        $password = I('post.password');
        empty($nickname) && $this->error('请输入昵称');
        empty($password) && $this->error('请输入密码');

        //密码验证
        $User   =   new UserApi();
        $uid    =   $User->login(UID, $password, 4);
        ($uid == -2) && $this->error('密码不正确');

        $Member =   D('Member');
        $data   =   $Member->create(array('nickname'=>$nickname));
        if(!$data){
            $this->error($Member->getError());
        }

        $res = $Member->where(array('uid'=>$uid))->save($data);

        if($res){
            $user               =   session('user_auth');
            $user['username']   =   $data['nickname'];
            session('user_auth', $user);
            session('user_auth_sign', data_auth_sign($user));
            $this->success('修改昵称成功！');
        }else{
            $this->error('修改昵称失败！');
        }
    }
    /**
     * 修改密码初始化
     * @author huajie <banhuajie@163.com>
     */
    public function updatePassword(){
        $this->meta_title = '修改密码';
        $this->display('updatepassword');
    }

    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function submitPassword(){
        //获取参数
        $password   =   I('post.old');
        empty($password) && $this->error('请输入原密码');
        $data['password'] = I('post.password');
        empty($data['password']) && $this->error('请输入新密码');
        $repassword = I('post.repassword');
        empty($repassword) && $this->error('请输入确认密码');

        if($data['password'] !== $repassword){
            $this->error('您输入的新密码与确认密码不一致');
        }

        $Api    =   new UserApi();
        $res    =   $Api->updateInfo(UID, $password, $data);
        if($res['status']){
            $this->success('修改密码成功！');
        }else{
            $this->error($res['info']);
        }
    }
}